package com.bluegalaxy.nhb.snapi.controller.api.ck;

import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.nhb.service.AnalyseSqlService;
import com.bluegalaxy.nhb.snservice.common.utils.DateUtil;
import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.domain.constant.AppConstant;
import com.bluegalaxy.nhb.snservice.domain.vo.api.R;
import com.bluegalaxy.nhb.snservice.domain.vo.api.ck.FunnelParamVO;
import com.bluegalaxy.nhb.snservice.domain.vo.api.ck.FunnelQueryResVO;
import com.bluegalaxy.nhb.snservice.domain.vo.api.ck.KeepParamVO;
import com.bluegalaxy.nhb.snservice.service.ck.FunnelService;
import com.bluegalaxy.nhb.snservice.service.ck.PathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.bluegalaxy.nhb.snservice.domain.constant.CkConstant.m_funnel;
import static com.bluegalaxy.nhb.snservice.domain.constant.CkConstant.queryItems;

/**
 * 用户路径分析
 */
@RestController
@RequestMapping(AppConstant.APPLICATION_NAME+"/path")
@Api(value = "用户路径分析", tags = "用户路径分析")
public class PathController {
    private static Log log = LogFactory.getLog(PathController.class);

    @Autowired
    private PathService pathService;
    @Resource
    private AnalyseSqlService sqlService;


    @ResponseBody
    @PostMapping("/findPath")
    @ApiOperation(value = "查询路径分析", notes = "查询路径分析列表")
    public R findPath(@RequestBody KeepParamVO paramVO) {
        log.info("findPath param参数："+JSONObject.toJSONString(paramVO));

        Integer windowsFunnel = null;


        List<String> dateLists = DateUtil.getBetweenStrDates(paramVO.getStartDate(),paramVO.getEndDate());
        paramVO.setDateList(dateLists);

        paramVO.setConditionSql(sqlService.analyseSql(paramVO.getJsonParam(),m_funnel,queryItems));

        List<Map<String,Object>> list = pathService.selectPathList(paramVO);


        return R.data(list);
    }






}
